//
//  IndexViewController.swift
//  SwiftProject
//
//  Created by HBY on 2021/4/29.
//

import UIKit

let CellIdentifier:String = "IndexTableViewCell"

class IndexViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    @IBOutlet weak var mTableView: UITableView!
    var pathArr:Array<String> = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        mTableView.register(UINib.init(nibName: CellIdentifier, bundle: nil), forCellReuseIdentifier: CellIdentifier)
        mTableView.separatorStyle = .none
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let fullPath = NSHomeDirectory().appending("/Documents/")
        let fileManager = FileManager.default
        
        DispatchQueue.global().async {
            self.pathArr = try! fileManager.contentsOfDirectory(atPath: fullPath)
            
            DispatchQueue.main.async { [self] in
                mTableView.reloadData()
            }
        }
    }
    
    func getImagePath(_ str:String) -> String {
        return NSHomeDirectory().appending("/Documents/").appending("\(str)")
    }

    //MARK: - UITableViewDelegate,UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return pathArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell:IndexTableViewCell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! IndexTableViewCell
        cell.titleLbl.text = pathArr[indexPath.row]
        cell.cellImg.image = UIImage.init(contentsOfFile: getImagePath(pathArr[indexPath.row]))
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let vc = ImageBrowseController()
        vc.imgArr = pathArr
        vc.edIndex = indexPath.row
        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let tempPath = getImagePath(pathArr[indexPath.row])
            let fileManager = FileManager.default
            do{
                try fileManager.removeItem(atPath: tempPath)
            }
            catch{
                view.makeToast("删除失败!")
            }

            pathArr.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .none)
        }
    }
}
